home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / math / newmat08 / newmatex.cpp < prev    next >
C/C++ Source or Header  |  1995-01-16  |  10KB  |  390 lines

  1. //$$ newmatex.cpp                    Exception handler
  2.  
  3. // Copyright (C) 1992,3,4: R B Davies
  4.  
  5. #define WANT_STREAM                  // include.h will get stream fns
  6.  
  7. #include "include.h"                 // include standard files
  8. #include "newmat.h"
  9.  
  10.  
  11. // action = -1    print message and exit(1)
  12. //           0    no message if handler available
  13. //           1    print message and use handler
  14.  
  15.  
  16.  
  17. int SpaceException::action = 1;           // these may be zero for global
  18. int DataException::action = 1;            // variables; probably doesn't
  19. int ConvergenceException::action = 1;     // matter
  20. int ProgramException::action = 1;
  21. int InternalException::action = 1;
  22.  
  23.  
  24. static inline iabs(int i) { return i >= 0 ? i : -i; }
  25.  
  26.  
  27. MatrixDetails::MatrixDetails(const GeneralMatrix& A)
  28.    : type(A.Type()), nrows(A.Nrows()), ncols(A.Ncols())
  29. { MatrixBandWidth bw = A.BandWidth(); ubw = bw.upper; lbw = bw.lower; }
  30.  
  31. void MatrixDetails::PrintOut()
  32. {
  33.     cout << "MatrixType = " << type.Value();
  34.    cout << "  # Rows = " << nrows;
  35.    cout << "; # Cols = " << ncols;
  36.    if (lbw >=0) cout << "; lower BW = " << lbw;
  37.    if (ubw >=0) cout << "; upper BW = " << ubw;
  38.    cout << "\n";
  39. }
  40.  
  41.  
  42.  
  43. SpaceException::SpaceException() : Exception(iabs(action))
  44. {
  45.    if (action) cout << "Out of space on heap\n";
  46.    if (action < 0) exit(1);
  47. }
  48.  
  49. MatrixException::MatrixException(int action) : Exception(iabs(action))
  50. { if (action) cout << "The exception is from newmat.\n"; }
  51.  
  52. MatrixException::MatrixException(int action, const GeneralMatrix& A)
  53.    : Exception(iabs(action))
  54. {
  55.    if (action)
  56.    {
  57.       cout << "The exception is from newmat: details of matrix follow:\n";
  58.       MatrixDetails(A).PrintOut();
  59.    }
  60. }
  61.  
  62. MatrixException::MatrixException(int action, const GeneralMatrix& A,
  63.    const GeneralMatrix& B) : Exception(iabs(action))
  64. {
  65.    if (action)
  66.    {
  67.       cout << "The exception is from newmat: details of matrices follow:\n";
  68.       MatrixDetails(A).PrintOut();
  69.       MatrixDetails(B).PrintOut();
  70.    }
  71. }
  72.  
  73. DataException::DataException(const GeneralMatrix& A)
  74.    : MatrixException(action, A) {}
  75.  
  76. NPDException::NPDException(const GeneralMatrix& A)
  77.    : DataException(A)
  78. {
  79.    if (action) cout << "The matrix is not positive definite\n\n";
  80.    if (action < 0) exit(1);
  81. }
  82.  
  83. SingularException::SingularException(const GeneralMatrix& A)
  84.    : DataException(A)
  85. {
  86.    if (action) cout << "The matrix is singular\n\n";
  87.    if (action < 0) exit(1);
  88. }
  89.  
  90. ConvergenceException::ConvergenceException(const GeneralMatrix& A)
  91.    : MatrixException(action,A)
  92. {
  93.    if (action) cout << "Process fails to converge\n\n";
  94.    if (action < 0) exit(1);
  95. }
  96.  
  97. ProgramException::ProgramException(char* c) : MatrixException(action)
  98. {
  99.    if (action) cout << c << "\n\n";
  100.    if (action < 0) exit(1);
  101. }
  102.  
  103. ProgramException::ProgramException(char* c, const GeneralMatrix& A)
  104.    : MatrixException(action,A)
  105. {
  106.    if (action) cout << c << "\n\n";
  107.    if (action < 0) exit(1);
  108. }
  109.  
  110. ProgramException::ProgramException(char* c, const GeneralMatrix& A,
  111.    const GeneralMatrix& B) : MatrixException(action,A,B)
  112. {
  113.    if (action) cout << c << "\n\n";
  114.    if (action < 0) exit(1);
  115. }
  116.  
  117. ProgramException::ProgramException(char* c, MatrixType a, MatrixType b)
  118.    : MatrixException(action)
  119. {
  120.    if (action)
  121.    {
  122.       cout << c << "\n";
  123.       cout << "MatrixType codes = " << +a << " " << +b << "\n\n";
  124.    }
  125.    if (action < 0) exit(1);
  126. }
  127.  
  128. ProgramException::ProgramException(const GeneralMatrix& A)
  129.    : MatrixException(action, A) {}
  130.  
  131. ProgramException::ProgramException(int) : MatrixException(action) {}
  132.  
  133. VectorException::VectorException() : ProgramException(0)
  134. {
  135.    if (action) cout << "Cannot convert matrix to vector\n\n";
  136.    if (action < 0) exit(1);
  137. }
  138.  
  139. VectorException::VectorException(const GeneralMatrix& A)
  140.    : ProgramException(A)
  141. {
  142.    if (action) cout << "Cannot convert matrix to vector\n\n";
  143.    if (action < 0) exit(1);
  144. }
  145.  
  146. NotSquareException::NotSquareException(const GeneralMatrix& A)
  147.    : ProgramException(A)
  148. {
  149.    if (action) cout << "Matrix is not square\n\n";
  150.    if (action < 0) exit(1);
  151. }
  152.  
  153. SubMatrixDimensionException::SubMatrixDimensionException()
  154.    : ProgramException(0)
  155. {
  156.    if (action) cout << "Incompatible submatrix dimension\n\n";
  157.    if (action < 0) exit(1);
  158. }
  159.  
  160. IncompatibleDimensionsException::IncompatibleDimensionsException()
  161.    : ProgramException(0)
  162. {
  163.    if (action) cout << "Incompatible dimensions\n\n";
  164.    if (action < 0) exit(1);
  165. }
  166.  
  167. NotDefinedException::NotDefinedException(char* op, char* matrix)
  168.    : ProgramException(0)
  169. {
  170.    if (action)
  171.       cout << "Operation " << op << " not defined for " << matrix << "\n\n";
  172.    if (action < 0) exit(1);
  173. }
  174.  
  175. CannotBuildException::CannotBuildException(char* matrix)
  176.    : ProgramException(0)
  177. {
  178.    if (action)
  179.       cout << "Cannot build matrix type " << matrix << "\n\n";
  180.    if (action < 0) exit(1);
  181. }
  182.  
  183. IndexException::IndexException(int i, const GeneralMatrix& A)
  184.    : ProgramException(A)
  185. {
  186.    if (action)
  187.       { cout << "Index error: requested index = " << i << "\n\n"; }
  188.    if (action < 0) exit(1);
  189. }
  190.  
  191. IndexException::IndexException(int i, int j, const GeneralMatrix& A)
  192.    : ProgramException(A)
  193. {
  194.    if (action)
  195.    {
  196.       cout << "Index error: requested indices = " << i << ", " << j << "\n\n";
  197.    }
  198.    if (action < 0) exit(1);
  199. }
  200.  
  201.  
  202. IndexException::IndexException(int i, const GeneralMatrix& A, Boolean)
  203.    : ProgramException(A)
  204. {
  205.    if (action)
  206.       { cout << "Element error: requested index (wrt 0) = " << i << "\n\n"; }
  207.    if (action < 0) exit(1);
  208. }
  209.  
  210. IndexException::IndexException(int i, int j, const GeneralMatrix& A, Boolean)
  211.    : ProgramException(A)
  212. {
  213.    if (action)
  214.    {
  215.       cout << "Element error: requested indices (wrt 0) = " 
  216.          << i << ", " << j << "\n\n";
  217.    }
  218.    if (action < 0) exit(1);
  219. }
  220.  
  221. InternalException::InternalException(char* c) : MatrixException(action)
  222. {
  223.    if (action) cout << c << "\n\n";
  224.    if (action < 0) exit(1);
  225. }
  226.  
  227.  
  228.  
  229.  
  230. /************************* ExeCounter functions *****************************/
  231.  
  232.  
  233.  
  234. int ExeCounter::nreports;                      // will be set to zero
  235.  
  236. ExeCounter::ExeCounter(int xl, int xf) : line(xl), fileid(xf), nexe(0) {}
  237.  
  238. ExeCounter::~ExeCounter()
  239. {
  240.    nreports++;
  241.    cout << nreports << "  " << fileid << "  " << line << "  " << nexe << "\n";
  242. }
  243.  
  244.  
  245.  
  246. /**************************** error handler *******************************/
  247.  
  248. void MatrixErrorNoSpace(void* v) { if (!v) Throw(SpaceException()); }
  249. // throw exception if v is null
  250.  
  251.  
  252.  
  253. /************************* test type manipulation **************************/
  254.  
  255.  
  256.  
  257.  
  258. // These functions may cause problems for Glockenspiel 2.0c; they are used
  259. // only for testing so you can delete them
  260.  
  261.  
  262. void TestTypeAdd()
  263. {
  264.    MatrixType list[9];
  265.    list[0] = MatrixType::UT;
  266.    list[1] = MatrixType::LT;
  267.    list[2] = MatrixType::Rt;
  268.    list[3] = MatrixType::Sm;
  269.    list[4] = MatrixType::Dg;
  270.    list[5] = MatrixType::BM;
  271.    list[6] = MatrixType::UB;
  272.    list[7] = MatrixType::LB;
  273.    list[8] = MatrixType::SB;
  274.  
  275.    cout << "+     ";
  276.     for (int i=0; i<MatrixType::nTypes(); i++) cout << list[i].Value() << " ";
  277.    cout << "\n";
  278.    for (i=0; i<MatrixType::nTypes(); i++)
  279.     {
  280.         cout << list[i].Value() << " ";
  281.       for (int j=0; j<MatrixType::nTypes(); j++)
  282.      cout << (list[j]+list[i]).Value() << " ";
  283.       cout << "\n";
  284.    }
  285.    cout << "\n";
  286. }
  287.  
  288. void TestTypeMult()
  289. {
  290.    MatrixType list[9];
  291.    list[0] = MatrixType::UT;
  292.    list[1] = MatrixType::LT;
  293.    list[2] = MatrixType::Rt;
  294.    list[3] = MatrixType::Sm;
  295.    list[4] = MatrixType::Dg;
  296.    list[5] = MatrixType::BM;
  297.    list[6] = MatrixType::UB;
  298.    list[7] = MatrixType::LB;
  299.    list[8] = MatrixType::SB;
  300.  
  301.    cout << "*     ";
  302.    for (int i=0; i<MatrixType::nTypes(); i++)
  303.         cout << list[i].Value() << " ";
  304.    cout << "\n";
  305.    for (i=0; i<MatrixType::nTypes(); i++)
  306.    {
  307.         cout << list[i].Value() << " ";
  308.       for (int j=0; j<MatrixType::nTypes(); j++)
  309.      cout << (list[j]*list[i]).Value() << " ";
  310.       cout << "\n";
  311.    }
  312.    cout << "\n";
  313. }
  314.  
  315. void TestTypeSP()
  316. {
  317.    MatrixType list[9];
  318.    list[0] = MatrixType::UT;
  319.    list[1] = MatrixType::LT;
  320.    list[2] = MatrixType::Rt;
  321.    list[3] = MatrixType::Sm;
  322.    list[4] = MatrixType::Dg;
  323.    list[5] = MatrixType::BM;
  324.    list[6] = MatrixType::UB;
  325.    list[7] = MatrixType::LB;
  326.    list[8] = MatrixType::SB;
  327.  
  328.    cout << "SP    ";
  329.    for (int i=0; i<MatrixType::nTypes(); i++)
  330.         cout << list[i].Value() << " ";
  331.    cout << "\n";
  332.    for (i=0; i<MatrixType::nTypes(); i++)
  333.    {
  334.         cout << list[i].Value() << " ";
  335.       for (int j=0; j<MatrixType::nTypes(); j++)
  336.      cout << (list[j].SP(list[i])).Value() << " ";
  337.       cout << "\n";
  338.    }
  339.    cout << "\n";
  340. }
  341.  
  342. void TestTypeOrder()
  343. {
  344.    MatrixType list[9];
  345.    list[0] = MatrixType::UT;
  346.    list[1] = MatrixType::LT;
  347.    list[2] = MatrixType::Rt;
  348.    list[3] = MatrixType::Sm;
  349.    list[4] = MatrixType::Dg;
  350.    list[5] = MatrixType::BM;
  351.    list[6] = MatrixType::UB;
  352.    list[7] = MatrixType::LB;
  353.    list[8] = MatrixType::SB;
  354.  
  355.    cout << ">=    ";
  356.    for (int i = 0; i<MatrixType::nTypes(); i++)
  357.         cout << list[i].Value() << " ";
  358.    cout << "\n";
  359.    for (i=0; i<MatrixType::nTypes(); i++)
  360.    {
  361.       cout << list[i].Value() << " ";
  362.       for (int j=0; j<MatrixType::nTypes(); j++)
  363.      cout << ((list[j]>=list[i]) ? "Yes   " : "No    ");
  364.       cout << "\n";
  365.    }
  366.    cout << "\n";
  367. }
  368.  
  369.  
  370. /************************* miscellanous errors ***************************/
  371.  
  372.  
  373. void CroutMatrix::GetRow(MatrixRowCol&)
  374.    { Throw(NotDefinedException("GetRow","Crout")); }
  375. void CroutMatrix::GetCol(MatrixRowCol&)
  376.    { Throw(NotDefinedException("GetCol","Crout")); }
  377. void CroutMatrix::operator=(const BaseMatrix&)
  378.    { Throw(NotDefinedException("=","Crout")); }
  379. void BandLUMatrix::GetRow(MatrixRowCol&)
  380.    { Throw(NotDefinedException("GetRow","BandLUMatrix")); }
  381. void BandLUMatrix::GetCol(MatrixRowCol&)
  382.    { Throw(NotDefinedException("GetCol","BandLUMatrix")); }
  383. void BandLUMatrix::operator=(const BaseMatrix&)
  384.    { Throw(NotDefinedException("=","BandLUMatrix")); }
  385. #ifdef TEMPS_DESTROYED_QUICKLY_R
  386.    ReturnMatrixX::ReturnMatrixX(const ReturnMatrixX& tm)
  387.      : gm(tm.gm) { Throw(ProgramException("ReturnMatrixX error")); }
  388. #endif
  389.  
  390.